//
//  ___PROJECTNAMEASIDENTIFIER___AppDelegate.mm
//  ___PROJECTNAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//  Copyright ___ORGANIZATIONNAME___ ___YEAR___. All rights reserved.
//

//File modified by Goffredo Marocchi (this is not the original SIO2 provided file,
//sio2 and its creator ROm are not responsible for the content of this file as it is now

#import "SysTools.h"
#import "png.h"

#import "___PROJECTNAMEASIDENTIFIER___AppDelegate.h"

@implementation ___PROJECTNAMEASIDENTIFIER___AppDelegate

@synthesize window;
@synthesize imageView;
@synthesize glView, SIO2GLobj;


BOOL backprojectionFix;
BOOL projectorEnable;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
	NSLog(@"Application Did finish Launching");
	
	enable_runfast(); //function defined in MathNeon, it enabled NEON's runfast mode.
	//testNEON();
	
	Average* avg = [[Average alloc] initWithNumber:0];
	[avg testAverage];
	
	//the object glView was created when the NIB file was unarchived and in turn it created the SIO2GLobj object.
	[self setSIO2GLobj:[glView renderer]];
	[SIO2GLobj setGlView:glView];
	
	backprojectionFix = YES;
	projectorEnable = YES;
	
	[SysTools testRightShift];
	//[SysTools testIsNumber]; //numbers parsing test
	
	// Flip the simulator to the right
	[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight
													  animated: NO];
	// Start the accelerometer with a rate of 60 fps
	[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0f/60.0f)];
    [[UIAccelerometer sharedAccelerometer] setDelegate:self];
	
	[SysTools sendOrientationNotifications:@selector(didRotateMethod:) to:self];
	
	[self setupFonts];
	
#if GL_OVERLAY_ON_UIKIT==1
	[SIO2GLobj makeGLViewTrasparent];
#endif
	
	[[DeviceInfo sharedDeviceInfo_instance] setFrameInterval:2];
	[SIO2GLobj setAnimationFrameInterval:[[DeviceInfo sharedDeviceInfo_instance] frameInterval]];
	[SIO2GLobj startAnimation];
	
	[[UIApplication sharedApplication] setIdleTimerDisabled:NO ];	
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
	NSLog(@"Application Did Become Active");
	[SIO2GLobj startAnimation];
}

- (void)applicationWillResignActive:(UIApplication *)application {
	NSLog(@"Application Will Resign Active");
	[SIO2GLobj stopAnimation];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    /*
     Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
     DidEnterBackground --> WillEnterForeground --> DidBecomeActive
	 */
	NSLog(@"Application has entered foreground");
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
     */
	NSLog(@"Application has entered background");
}

- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate and multi-tasking is not supported.
     See also applicationDidEnterBackground:.
     */
	[SIO2GLobj stopAnimation];
	NSLog(@"Application Will Terminate... iOS < 4.0 or backgrounding disabled");
}

- (void)didRotateMethod:(NSNotification*) sender {
	
	UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
	
	switch (deviceOrientation) {
		case UIDeviceOrientationUnknown:
			break;
		case UIDeviceOrientationLandscapeLeft:
			NSLog(@"Landscape left");
			break;
		case UIDeviceOrientationLandscapeRight:
			NSLog(@"Landscape right");
			break;
		case UIDeviceOrientationPortrait:
			NSLog(@"Portrait up");
			break;
		case UIDeviceOrientationPortraitUpsideDown:
			NSLog(@"Portrait down");
			break;
		case UIDeviceOrientationFaceUp:
			NSLog(@"Face up");
			break;
		case UIDeviceOrientationFaceDown:
			NSLog(@"Face Down");
			break;
		default:
			NSLog(@"deviceOrientation = %i", deviceOrientation);
			NSAssert(FALSE, @"Wrong notification delivered... not a valid UIDeviceOrientation notification");
			break;
	}
}

- (void)dealloc {
	SAFE_FREE (font);
	[window release];
	[SIO2GLobj release];
	[super dealloc];
}


// Create an handler that is going to help us dispatch
// the accelerometer events to our main window event
// structure. Remember to extend your main class to support
// the acceleromter ( templateAppDelegate.h line #16 )
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
	// Uncomment this block for having a smooth accelerometer
	// interpolation, instead well... its a bit jumpy :)
	/*
	 sio2->_SIO2window->accel->x = acceleration.x * ( 1.0f - sio2->_SIO2window->accel_smooth ) + sio2->_SIO2window->accel->x * sio2->_SIO2window->accel_smooth;
	 sio2->_SIO2window->accel->y = acceleration.y * ( 1.0f - sio2->_SIO2window->accel_smooth ) + sio2->_SIO2window->accel->y * sio2->_SIO2window->accel_smooth;
	 sio2->_SIO2window->accel->z = acceleration.z * ( 1.0f - sio2->_SIO2window->accel_smooth ) + sio2->_SIO2window->accel->z * sio2->_SIO2window->accel_smooth;	
	 
	 sio2->_SIO2window->accel->x = ( int )( sio2->_SIO2window->accel->x * 100.0f );
	 sio2->_SIO2window->accel->x *= 0.01f;
	 
	 sio2->_SIO2window->accel->y = ( int )( sio2->_SIO2window->accel->y * 100.0f );
	 sio2->_SIO2window->accel->y *= 0.01f;		
	 */
	
	sio2->_SIO2window->accel->x = acceleration.x;
	sio2->_SIO2window->accel->y = acceleration.y;
	sio2->_SIO2window->accel->z = acceleration.z;	
	
	sio2ResourceDispatchEvents( sio2->_SIO2resource,
							   sio2->_SIO2window,
							   SIO2_WINDOW_ACCELEROMETER,
							   SIO2_WINDOW_TAP_NONE );
}

- (void) setupFonts {
	NSString *fontpath = [NSString stringWithFormat:@"%@/Diavlo_BLACK_II_37.otf", 
						  [[NSBundle mainBundle] resourcePath]];
	
	font = new FTTextureFont([fontpath UTF8String]);
	if (font->Error())
	{
        NSLog(@"Could not load font `%@'\n", fontpath);	
	}
	font->FaceSize(16);
}


- (void) renderSampleText {
//	glClear(GL_COLOR_BUFFER_BIT);
//	glMatrixMode(GL_PROJECTION);
//    glLoadIdentity();
//	glOrthof(0.0f, screenSize.width, 0.0f, screenSize.height, 10.0f, -10.0f);
//	glMatrixMode(GL_MODELVIEW);
//	glLoadIdentity();
//	
//	glTranslatef(0.0f, screenSize.height * 0.5f, 0.0f);
	glColor4f(1.0f, 0.6f, 0.3f, 1.0f);
	if (font) {
		font->Render("Hello world ç ò à é  è  ì $ £ ù!");
	}
}

-(IBAction) projectorSwitch:(id)sender {
	projectorEnable = projectorEnable? FALSE : TRUE;
}
-(IBAction) backprojectionFixSwitch:(id)sender {
	backprojectionFix = backprojectionFix? FALSE : TRUE;
}

- (SIO2GL*) getSIO2GL_object {
	return SIO2GLobj;
}


@end
